//
//  VisitorView.swift
//  Sinaweibo
//
//  Created by 谌桃 on 15/11/28.
//  Copyright © 2015年 ryan.chen. All rights reserved.
//

import UIKit

class VisitorView: UIView {
    
    //提供给外界访问的方法
    func setWithInfo(tipLabel:String, imageName:String?){
        
        label.text = tipLabel
        
        //不是首页, 不为空就进来
        if let imageName = imageName{
            
        largeImage.image = UIImage(named: imageName)
            
        houseImage.hidden = true
            
        //将圆圈放在最顶层
        bringSubviewToFront(largeImage)
       
        }else{
        
            //首页的圆圈视图 添加动画效果
            setAnimation()
        }
    }

    //给圆圈创建一个基础动画
    private func setAnimation(){
    
        //让圆圈旋转起来
       let animation =  CABasicAnimation(keyPath:"transform.rotation" )
        
        //持续时间
        animation.duration = 10.0
        
        //重复次数
        animation.repeatCount = MAXFLOAT
        
        //终点
        animation.toValue = 2 * M_PI
        
        //完成动画 不移除图层
        animation.removedOnCompletion = false
        
        //添加到Layer
        largeImage.layer.addAnimation(animation, forKey: nil)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubviews()
    }

    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        addSubviews()
    }
    
    private func addSubviews() {
        
        addSubview(largeImage)
        addSubview(backImage)
        addSubview(houseImage)
        addSubview(label)
        addSubview(loginBtn)
        addSubview(registerBtn)
       
        
        //使用可视化布局, VFL, 必须关闭默认的布局, 设置的布局才有作用
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant"
        
        //圆圈
        addConstraint(NSLayoutConstraint(item: largeImage, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: largeImage, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        //房子
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: .CenterX, relatedBy: .Equal, toItem: largeImage, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImage, attribute: .CenterY, relatedBy: .Equal, toItem: largeImage, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //文字
        addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: largeImage, attribute: .CenterX, multiplier: 1, constant: 0))
       
        addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: largeImage, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: label, attribute:.Width, relatedBy: .Equal, toItem: nil, attribute:.NotAnAttribute, multiplier: 1, constant: 230))
        
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: label, attribute: .Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: label, attribute: .Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置蒙版视图可视化 布局格式
        
        //水平格式
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: [], metrics: nil, views: ["maskView":backImage]))
        
        //竖直格式
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-[loginB]", options: [], metrics: nil, views: ["maskView":backImage,"loginB":loginBtn]))

        //设置背景颜色 
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
    }
    
    //懒加载, View的子控件
    
    //圆圈
    private lazy var largeImage:UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //房子
    private lazy var houseImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //蒙版视图
    private lazy var backImage: UIImageView = UIImageView( image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //文字
    private lazy var label:UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜,关注一些人，回这里看看有什么惊喜"
        
        label.textAlignment = NSTextAlignment.Center
        
        //设置换行
        label.numberOfLines = 0
        
        label.sizeToFit()
        
        return label
    }()
    
    
    //登录按钮
    private var loginBtn:UIButton = {
        
        let loginBtn = UIButton()
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        loginBtn.setTitle("登录", forState: .Normal)
        
        loginBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        return loginBtn
        
        }()
    
    
    //注册按钮
    private var registerBtn:UIButton = {
        
        let registerBtn = UIButton()
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        registerBtn.setTitle("注册", forState: .Normal)
        
        registerBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        return registerBtn
        
        }()
    
}
